/**
 * @description 订单控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const model = require("../../model/order/ORDER.MODEL")
const { getFilter } = require("../../utils/setFilter")
const { generateSixDigitID } = require("../../utils/crypto")
const dayjs = require("dayjs")

// 创建一个订单
const controller_save = async ({
    orderId = undefined,
    products = undefined,
    buyerUserId = undefined,
    buyerAddress = undefined,
}) => {
    let time = dayjs();
    const params = {
        createDate: time,
        orderId: `${dayjs(time).format("YYYYMMDDHHmmss")}${generateSixDigitID(6)}`, products, buyerUserId, buyerAddress
    }
    let res = await model.create({ ...params })
    if (res) {
        const {
            createDate, orderId, products, buyerUserId, buyerAddress,
        } = res

        return {
            code: 0,
            msg: "下单成功",
            data: {
                createDate, orderId, products, buyerUserId, buyerAddress,
            }
        }
    }
    return {
        code: 100,
        msg: "下单失败",
    }

}


// 查询列表 
const controller_find = async ({ current, pageSize, orderStatus, payStatus, buyerUserId, orderId }) => {

    try {
        let params = {
            orderStatus, payStatus, buyerUserId, orderId
        }
        let filter = getFilter(params)
        const list = await model.find(filter).sort({ createDate: 0 }).skip((current - 1) * pageSize).limit(pageSize);
        const totalCount = await model.countDocuments(filter);
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 1,
            msg: "查询失败",
        }
    }
    // return list;
}


/**@description 根据id查详情 */
const controller_get = async ({ id, title }) => {
    try {
        const res = await model.findOne({
            id
        })
        if (res) {
            // console.log("产品详情查询：", res);
            const { id, title, subjectId, mainImg, description, status } = res;

            return {
                code: 0,
                msg: "查询成功",
                data: {
                    id, title, subjectId, mainImg, description, status
                }
            }
        } else {
            return {
                code: 100,
                msg: "未找到对应文章数据"
            }
        }
    } catch (error) {
        console.log("查详情报错：", error)
        return {
            code: 100,
            msg: "查详情报错" + error
        }
    }
}

// 删除
const controller_delete = async ({ ids = [], id = undefined }) => {
    try {

        const res = await model.findOneAndUpdate({ id: id }, {
            $set: {
                deleteFlag: true
            }
        }, { new: true })

        if (res) {
            return {
                code: 0,
                msg: `删除成功`
            }
        } else {
            return {
                code: 100,
                msg: `删除失败`
            }
        }
    } catch (error) {
        console.log("删除控制器出错", error)
        return {
            code: 100,
            msg: `删除出错` + error
        }

    }

}
module.exports = {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
}